﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Aphysoft.Vate.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Aphysoft.Vate.Editor
{
    public partial class VateEditor : IGraphicsDeviceService 
    {
        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Setup Presentation Parameters.
        /// </summary>
        /// <returns></returns>
        private PresentationParameters PresentationParametersSetup()
        {
            PresentationParameters pp = new PresentationParameters();

            // No full screen
            pp.IsFullScreen = false;
            pp.BackBufferCount = 1;

            pp.BackBufferWidth = RenderPanel.Width;
            pp.BackBufferHeight = RenderPanel.Height;

            pp.AutoDepthStencilFormat = DepthFormat.Depth24;
            pp.EnableAutoDepthStencil = true;

            pp.PresentationInterval = PresentInterval.Immediate;
            pp.BackBufferFormat = SurfaceFormat.Color;
            pp.MultiSampleType = MultiSampleType.None;
            pp.RenderTargetUsage = RenderTargetUsage.DiscardContents;
            pp.SwapEffect = SwapEffect.Discard;

            return pp;
        }
        /// <summary>
        /// Graphics setup.
        /// </summary>
        private void GraphicsSetup()
        {
            // Create Presentation Parameters
            PresentationParameters pp = PresentationParametersSetup();

            // Create device
            GraphicsDevice device;

            device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, DeviceType.Hardware, RenderPanel.Handle, pp);

            device.DeviceReset += new EventHandler(GraphicsDeviceReset);

            Vate.Width = RenderPanel.Width;
            Vate.Height = RenderPanel.Height;
            Vate.DefaultViewport = device.Viewport;

            Vate.SetGraphicsDevice(device);
        }
        /// <summary>
        /// Raised when GraphicsDeviceManager is reset.
        /// </summary>
        private void GraphicsDeviceReset(object sender, EventArgs e)
        {
            // Update master width and height
            Vate.Width = RenderPanel.Width;
            Vate.Height = RenderPanel.Height;

            // Update default projector
            Vate.DefaultViewport = Vate.Device.Viewport;

            foreach (KeyValuePair<string, Projector> projectorEntry in ProjectorManager.Projectors)
            {
                string gameProjectorName = projectorEntry.Key;
                Projector gameProjector = projectorEntry.Value;

                gameProjector.Update();
            }
#if DEBUG
            Debug.Write("GraphicsDevice Reset.");
#endif
        }
        #endregion

        #region IGraphicsDeviceService Members

        public event EventHandler DeviceCreated;

        public event EventHandler DeviceDisposing;

        public event EventHandler DeviceReset;

        public event EventHandler DeviceResetting;

        public GraphicsDevice GraphicsDevice
        {
            get { return Vate.Device; }
        }

        #endregion
    }
}
